<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Backend extends MX_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function __construct()
	{
		parent::__construct();	
		$this->display->set_theme('cms');
		
	}
	
	public function logs(){	$this->load->spark('fire_log/0.8.2'); }
	public function index()
	{
		permission('cms', 'dashboard');
		$data['analytics']['start_date'] = (date("Y") - 1) . "-" . date("m") . "-" . date("d");
		$data['analytics']['end_date'] = date("Y") . "-" . date("m") . "-" . date("d");
		
		$this->twiggy->title()->append('Dashboard');
		$this->display->template('dashboard', $data);
	}
	
	public function config()
	{
		permission('cms', 'config');
		$this->load->model('config_model');
		$config = $this->config_model->get_by_categorie();
		$this->twiggy->title()->append('Configuration');
		$this->display->template('config', array('config' => $config));		
	}
	public function analytics()
	{
		$this->display->template('analytics');		
	}
	
	public function model_generator($table)
	{
	$fields = $this->db->field_data($table);
	echo	$qualification_fields = '\''.implode('\', \'', $this->db->list_fields($table)) .'\'';
	$primary_key = '';
	foreach ($fields as $field)
	{
		if($field->primary_key)
		{
			$primary_key = $field->name;
		}
		
	} 	
	
		
$stuff = @"
class ".ucfirst(strtolower($table))."_model extends CI_Model
{	
	function __construct()
	{
		parent::__construct();
		
	}
	private function _required(\$required, \$data)
	{
		foreach(\$required as \$field) if(!isset(\$data[\$field])) return false;
		return true;
	}
	
	private function _default(\$defaults, \$options)
	{
		return array_merge(\$defaults, \$options);
	}
	
	function insert(\$options = array())
	{
		//if(!\$this->_required(array(.\$qualification_fields.), \$options)) 
		//		return false;
	
		//\$options = \$this->_default(array('u_created_on' => sql_current_datetime()), \$options);
		\$qualificationArray = array(".$qualification_fields.");
		foreach(\$qualificationArray as \$qualifier)
			if(isset(\$options[\$qualifier])) 
				\$this->db->set(\$qualifier, \$options[\$qualifier]);
	 
		\$this->db->insert('".$table."');
	
		return \$this->db->insert_id();
	}
	
	function update(\$options = array())
	{
		if(!\$this->_required(array('".$primary_key."'), \$options)) 
			return false;

		\$qualificationArray = array(".$qualification_fields.");
		foreach(\$qualificationArray as \$qualifier)
			if(isset(\$options[\$qualifier])) 
				\$this->db->set(\$qualifier, \$options[\$qualifier]);
	
		\$this->db->where('".$primary_key."', \$options [ '".$primary_key."' ]);	
		\$this->db->update('".$table."');	
		return \$this->db->affected_rows();
	}
	
	function get(\$options = array())
	{
		\$options = \$this->_default(array('sort_direction' => 'asc'), \$options);
	
		\$qualificationArray = array(".$qualification_fields.");
		foreach(\$qualificationArray as \$qualifier)
			if(isset(\$options[\$qualifier])) 
				\$this->db->where(\$qualifier, \$options[\$qualifier]);
	
		if(isset(\$options ['limit']) && isset(\$options ['offset'])) 
			\$this->db->limit(\$options ['limit'], \$options ['offset']);
		else if(isset(\$options ['limit'])) 
			\$this->db->limit(\$options ['limit']);
	
		if(isset(\$options ['sort_by'])) 
		\$this->db->order_by(\$options ['sort_by'], \$options ['sort_direction']);
		
		\$query = \$this->db->get('".$table."');
		
		if(\$query->num_rows() == 0) 
			return false;
	
		if(isset(\$options ['".$primary_key."']))
			return \$query->row_array(0);
		else
			return \$query->result_array();
	}
	
	function delete(\$options = array())
	{
		if(!\$this->_required(array('".$primary_key."'), \$options)) 
			return false;
	
		\$this->db->where('".$primary_key."', \$options ['".$primary_key."']);
		\$this->db->delete('".$table."');
	}

}
";		

	$this->output->set_output($stuff);
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */